/* $Revision: $ $Date: $
 * 
 * Copyright (C) 2009 Robert Stephan
 * 
 * This file is part of *** CaCaO - Card Catalogs Online ***
 *
 * CaCaO is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * CaCao is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package cacao.client.ui;

import cacao.client.CaCaO;
import cacao.client.data.CaCaOHelper;
import cacao.client.data.OrderObject;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.TextBoxBase;
import com.google.gwt.user.client.ui.VerticalPanel;


public class OrderDialog extends DialogBox {
	private CaCaO application;
	private  DeckPanel dpContent;
	private Button btnOPAC, btnOrderForm, btnOrder, btnCancel;
	private TextBox txtName, txtFirstName, txtID, txtEmail, txtIssue;
	private TextArea txtComment;

	public OrderDialog(CaCaO app) {
		application = app;
		String catalogCode=application.getCurrentCatalog().getCode();
		setText(application.getMessages().orderDialogWindowTitle());
		dpContent = new DeckPanel();
		// Set the dialog box's caption.
		if("true".equals(application.getProperties().get("CaCaO.client.orderform.checkOPAC."+catalogCode))){
			DockPanel dpCheckOPAC = new DockPanel();
			HTML htmlHead = new HTML(application.getMessages().orderDialogHeadline());
			dpCheckOPAC.add(htmlHead, DockPanel.NORTH);
			HTML htmlBody = new HTML(application.getMessages().orderDialogInformation());

			dpCheckOPAC.add(htmlBody, DockPanel.CENTER);
			HorizontalPanel hpCheckOPACButtons = new HorizontalPanel();
			hpCheckOPACButtons.setWidth("100%");
			btnOPAC = new Button(application.getMessages().orderDialogToOPAC());
			btnOPAC.addClickHandler(new ClickHandler(){
				public void onClick(ClickEvent evt) {
					Window.open(application.getProperties().get("CaCaO.client.opacbaseurl"), "_blank", "alwaysRaised=yes,menubar=yes,toolbar=yes,location=yes,resizable=yes,status=yes,scrollbars=yes");
					hide();
				}
			});
			hpCheckOPACButtons.add(btnOPAC);
			btnOrderForm = new Button(application.getMessages().orderDialogToOrderForm());
			btnOrderForm.addClickHandler(new ClickHandler(){
				public void onClick(ClickEvent evt){
					dpContent.showWidget(1);
				}
			});
			hpCheckOPACButtons.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
			hpCheckOPACButtons.add(btnOrderForm);
			dpCheckOPAC.add(hpCheckOPACButtons, DockPanel.SOUTH);
			dpContent.add(dpCheckOPAC);	    	  
		}

		DockPanel dpUserData = new DockPanel();
		HorizontalPanel hpOrderButtons = new HorizontalPanel();
		hpOrderButtons.setWidth("100%");
		btnOrder = new Button(application.getMessages().orderFormOrder());
		btnOrder.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent evt){
				executeOrder();
			}
		});
		hpOrderButtons.add(btnOrder);
		hpOrderButtons.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		btnCancel = new Button(application.getMessages().orderFormCancel());
		btnCancel.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent evt){
				hide();
			}
		});
		hpOrderButtons.add(btnCancel);
		dpUserData.add(hpOrderButtons, DockPanel.SOUTH);
		dpUserData.add(new HTML(application.getMessages().orderFormIntro()), DockPanel.NORTH);

		String html = CaCaOHelper.getCatalogSpecificString(application.getMessages().orderFormInformation_JSON(), catalogCode);
		HTML htmlInfo = new HTML(html);
		dpUserData.add(htmlInfo, DockPanel.SOUTH);

		VerticalPanel vpData = new VerticalPanel();
		txtName = new TextBox();
		txtFirstName = new TextBox();
		txtID = new TextBox();
		txtEmail = new TextBox();
		txtIssue = new TextBox();
		txtComment = new TextArea();
		addInputline(vpData, application.getMessages().orderFormFieldLastname()+":", txtName,null);
		addInputline(vpData, application.getMessages().orderFormFieldFirstname()+":", txtFirstName,null);
		addInputline(vpData, application.getMessages().orderFormFieldUserID()+":", txtID,null);
		addInputline(vpData, application.getMessages().orderFormFieldEmail()+":", txtEmail,null);
		vpData.add(new HTML("<hr />"));

		String data1 = CaCaOHelper.getCatalogSpecificString(application.getMessages().orderFormFieldData_JSON(), catalogCode);
		addInputline(vpData, data1+":", txtIssue,null);	      

		addInputline(vpData, application.getMessages().orderFormFieldComment()+":", txtComment,"90px");
		vpData.add(new HTML("&nbsp;"));

		dpUserData.add(vpData, DockPanel.CENTER);

		dpContent.add(dpUserData);
		dpContent.showWidget(0);
		setWidget(dpContent);
		
		setPopupPositionAndShow(new PopupPanel.PositionCallback() {
			public void setPosition(int offsetWidth, int offsetHeight) {
				int left = ((Window.getClientWidth() - offsetWidth) / 2) >> 0;
				int top = ((Window.getClientHeight() - offsetHeight) / 2) >> 0;
				setPopupPosition(left, top);
			}
		}); 
	}

	private void addInputline(VerticalPanel vp, String label, TextBoxBase text, String height){
		HorizontalPanel hp = new HorizontalPanel();
		hp.setWidth("100%");
		Label lbl = new Label(label);
		lbl.setWidth("175px");
		hp.add(lbl);
		text.setWidth("300px");
		if(height!=null){
			text.setHeight(height);
		}
		hp.add(text);
		vp.add(hp);
	}

	private void executeOrder(){
		if(txtName.getText().length()==0 || txtFirstName.getText().length()==0 || txtID.getText().length()==0 || txtEmail.getText().length()==0){
			Window.alert(application.getMessages().orderMessageFillOutEverything());
		}
		else{
			AsyncCallback<String> callback = new AsyncCallback<String>(){
				public void onSuccess(String result){
					if(result ==null){
						//login successful
						Window.alert(application.getMessages().orderMessageSuccess());
						hide();
					}
					else{
						Window.alert(result);
						hide();
					}	
				}
				public void onFailure(Throwable caught){
					Window.alert(application.getMessages().orderMessageFailure());
				}
			};
			OrderObject order = new OrderObject(application.getCurrentCatalog().getName(),
					application.getCurrentItem().getId(),
					application.getCurrentCatalog().getExtension(),
					txtName.getText(),
					txtFirstName.getText(),
					txtID.getText(),
					txtEmail.getText(),
					txtIssue.getText(),
					txtComment.getText());
			application.getDataService().executeOrder(order, callback);
		}
	}
}  


